package com.ruoyi.drone.controller;

import java.math.BigDecimal;
import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.drone.domain.DroneTaskExecution;
import com.ruoyi.drone.service.IDroneTaskExecutionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 任务执行Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/drone/execution")
public class DroneTaskExecutionController extends BaseController
{
    @Autowired
    private IDroneTaskExecutionService droneTaskExecutionService;

    /**
     * 查询任务执行列表
     */
    @PreAuthorize("@ss.hasPermi('drone:execution:list')")
    @GetMapping("/list")
    public TableDataInfo list(DroneTaskExecution droneTaskExecution)
    {
        startPage();
        List<DroneTaskExecution> list = droneTaskExecutionService.selectDroneTaskExecutionList(droneTaskExecution);
        return getDataTable(list);
    }

    /**
     * 根据任务ID查询任务执行列表
     */
    @PreAuthorize("@ss.hasPermi('drone:execution:list')")
    @GetMapping("/listByTaskId/{taskId}")
    public TableDataInfo listByTaskId(@PathVariable("taskId") Long taskId)
    {
        startPage();
        List<DroneTaskExecution> list = droneTaskExecutionService.selectDroneTaskExecutionListByTaskId(taskId);
        return getDataTable(list);
    }

    /**
     * 根据飞手ID查询任务执行列表
     */
    @PreAuthorize("@ss.hasPermi('drone:execution:list')")
    @GetMapping("/listByPilotId/{pilotId}")
    public TableDataInfo listByPilotId(@PathVariable("pilotId") Long pilotId)
    {
        startPage();
        List<DroneTaskExecution> list = droneTaskExecutionService.selectDroneTaskExecutionListByPilotId(pilotId);
        return getDataTable(list);
    }

    /**
     * 导出任务执行列表
     */
    @PreAuthorize("@ss.hasPermi('drone:execution:export')")
    @Log(title = "任务执行", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DroneTaskExecution droneTaskExecution)
    {
        List<DroneTaskExecution> list = droneTaskExecutionService.selectDroneTaskExecutionList(droneTaskExecution);
        ExcelUtil<DroneTaskExecution> util = new ExcelUtil<DroneTaskExecution>(DroneTaskExecution.class);
        util.exportExcel(response, list, "任务执行数据");
    }

    /**
     * 获取任务执行详细信息
     */
    @PreAuthorize("@ss.hasPermi('drone:execution:query')")
    @GetMapping(value = "/{executionId}")
    public AjaxResult getInfo(@PathVariable("executionId") Long executionId)
    {
        return success(droneTaskExecutionService.selectDroneTaskExecutionByExecutionId(executionId));
    }

    /**
     * 根据申请ID获取任务执行信息
     */
    @PreAuthorize("@ss.hasPermi('drone:execution:query')")
    @GetMapping(value = "/getByApplicationId/{applicationId}")
    public AjaxResult getByApplicationId(@PathVariable("applicationId") Long applicationId)
    {
        return success(droneTaskExecutionService.selectDroneTaskExecutionByApplicationId(applicationId));
    }

    /**
     * 根据任务ID和飞手ID获取任务执行信息
     */
    @PreAuthorize("@ss.hasPermi('drone:execution:query')")
    @GetMapping(value = "/getByTaskAndPilot/{taskId}/{pilotId}")
    public AjaxResult getByTaskAndPilot(@PathVariable("taskId") Long taskId, @PathVariable("pilotId") Long pilotId)
    {
        return success(droneTaskExecutionService.selectDroneTaskExecutionByTaskIdAndPilotId(taskId, pilotId));
    }

    /**
     * 新增任务执行
     */
    @PreAuthorize("@ss.hasPermi('drone:execution:add')")
    @Log(title = "任务执行", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DroneTaskExecution droneTaskExecution)
    {
        return toAjax(droneTaskExecutionService.insertDroneTaskExecution(droneTaskExecution));
    }

    /**
     * 开始任务执行
     */
    @PreAuthorize("@ss.hasPermi('drone:execution:start')")
    @Log(title = "开始任务执行", businessType = BusinessType.INSERT)
    @PostMapping("/start")
    public AjaxResult startExecution(@RequestBody DroneTaskExecution droneTaskExecution)
    {
        try {
            return toAjax(droneTaskExecutionService.startTaskExecution(droneTaskExecution));
        } catch (RuntimeException e) {
            return error(e.getMessage());
        }
    }

    /**
     * 修改任务执行
     */
    @PreAuthorize("@ss.hasPermi('drone:execution:edit')")
    @Log(title = "任务执行", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DroneTaskExecution droneTaskExecution)
    {
        return toAjax(droneTaskExecutionService.updateDroneTaskExecution(droneTaskExecution));
    }

    /**
     * 更新工作进度
     */
    @PreAuthorize("@ss.hasPermi('drone:execution:progress')")
    @Log(title = "更新工作进度", businessType = BusinessType.UPDATE)
    @PutMapping("/progress/{executionId}")
    public AjaxResult updateProgress(@PathVariable("executionId") Long executionId, @RequestBody DroneTaskExecution execution)
    {
        return toAjax(droneTaskExecutionService.updateWorkProgress(
            executionId, 
            execution.getCompletionRate(), 
            execution.getWorkContent(), 
            execution.getWorkImages(), 
            execution.getWorkVideos(), 
            execution.getWorkHours()
        ));
    }

    /**
     * 完成任务执行
     */
    @PreAuthorize("@ss.hasPermi('drone:execution:complete')")
    @Log(title = "完成任务执行", businessType = BusinessType.UPDATE)
    @PutMapping("/complete/{executionId}")
    public AjaxResult completeExecution(@PathVariable("executionId") Long executionId, @RequestBody DroneTaskExecution execution)
    {
        return toAjax(droneTaskExecutionService.completeTaskExecution(
            executionId, 
            execution.getFinalPrice(), 
            execution.getQualityScore()
        ));
    }

    /**
     * 暂停任务执行
     */
    @PreAuthorize("@ss.hasPermi('drone:execution:pause')")
    @Log(title = "暂停任务执行", businessType = BusinessType.UPDATE)
    @PutMapping("/pause/{executionId}")
    public AjaxResult pauseExecution(@PathVariable("executionId") Long executionId)
    {
        return toAjax(droneTaskExecutionService.pauseTaskExecution(executionId));
    }

    /**
     * 恢复任务执行
     */
    @PreAuthorize("@ss.hasPermi('drone:execution:resume')")
    @Log(title = "恢复任务执行", businessType = BusinessType.UPDATE)
    @PutMapping("/resume/{executionId}")
    public AjaxResult resumeExecution(@PathVariable("executionId") Long executionId)
    {
        return toAjax(droneTaskExecutionService.resumeTaskExecution(executionId));
    }

    /**
     * 取消任务执行
     */
    @PreAuthorize("@ss.hasPermi('drone:execution:cancel')")
    @Log(title = "取消任务执行", businessType = BusinessType.UPDATE)
    @PutMapping("/cancel/{executionId}")
    public AjaxResult cancelExecution(@PathVariable("executionId") Long executionId)
    {
        return toAjax(droneTaskExecutionService.cancelTaskExecution(executionId));
    }

    /**
     * 删除任务执行
     */
    @PreAuthorize("@ss.hasPermi('drone:execution:remove')")
    @Log(title = "任务执行", businessType = BusinessType.DELETE)
	@DeleteMapping("/{executionIds}")
    public AjaxResult remove(@PathVariable Long[] executionIds)
    {
        return toAjax(droneTaskExecutionService.deleteDroneTaskExecutionByExecutionIds(executionIds));
    }

    /**
     * 检查任务执行是否唯一
     */
    @PreAuthorize("@ss.hasPermi('drone:execution:query')")
    @GetMapping("/checkUnique/{taskId}/{pilotId}")
    public AjaxResult checkUnique(@PathVariable("taskId") Long taskId, @PathVariable("pilotId") Long pilotId)
    {
        boolean isUnique = droneTaskExecutionService.checkTaskExecutionUnique(taskId, pilotId);
        return success(isUnique);
    }
}
